package lion.core;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import lion.codec.IClientDispatcher;
import lion.message.MyRequestMsg;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.jboss.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClientCallPool {

	private static Logger logger = LoggerFactory.getLogger(ClientCallPool.class);

	private ExecutorService threadPool;

	private IClientDispatcher clientDispatcher;

	public ClientCallPool(IClientDispatcher clientDispatcher) {
		BasicThreadFactory factory = new BasicThreadFactory.Builder().namingPattern("logic-thread").daemon(true).priority(Thread.MAX_PRIORITY).build();
		threadPool = Executors.newFixedThreadPool(10, factory);
		this.clientDispatcher = clientDispatcher;
	}

	public void execute(final Channel channel, Object remoteObj) {
		final MyRequestMsg request = (MyRequestMsg) remoteObj;
		threadPool.execute(new Runnable() {
			@Override
			public void run() {
				try {
					clientDispatcher.executeInPool(channel, request);
				} catch (Exception e) {
					logger.error("logic pool execute error!", e);
				}
			}
		});
	}
}
